﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsFormsApplication1.WebTest.WebTestXmlNodes.New
{
    [XmlRoot("TransactionTimer", Namespace = XmlNamespace)]
    public class TransactionTimerNode : GenericWebTestNode, IXmlSerializable
    {
        private GenericWebTestNode _items;

        public override void SetNodeProperties()
        {
            SelectedImageIndex = 14;
            ImageIndex = 14;

            Text = _nodeProperties["Name"].Value;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Notification.BeginOperation("Processing TransactionTimer Node", NodeOperationEventArgs.EventImportance.Detailed);

            //Read TransactionTimer attributes
            ReadXmlAttributes(reader);

            //Set node properties such as Icon, Text, etc..
            SetNodeProperties();

            //Move to the first element
            reader.MoveToContent();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.Read();
            reader.MoveToContent();


            bool exitLoop = false;
            while (!isEmptyElement && reader.NodeType != XmlNodeType.EndElement)
            {
                //Process sub tags
                switch (reader.Name)
                {
                    case "Items":
                        ItemsNode items =
                            (ItemsNode)_itemsNodeSerializer.Deserialize(reader);

                        foreach (GenericWebTestNode requestNode in items.Nodes)
                            Nodes.Add(requestNode);

                        _items = items;

                        break;

                    default:
                        Notification.DebugMessage("Unknown tag: " + reader.Name);
                        exitLoop = true;
                        break;

                }

                reader.MoveToContent();
                if (exitLoop) break;
            }


            //If there is closing tag read it
            if (!isEmptyElement)
                reader.ReadEndElement();

            Notification.EndOperation();
        }


        #endregion

    }
}
